package com.lv.service.room.category

import android.content.Context
import android.os.Parcelable
import androidx.annotation.Keep
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.Update
import com.github.gzuliyujiang.wheelview.contract.TextProvider
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(entities = [NewTelCategoryBean::class], version = 1, exportSchema = false)
abstract class NewTelCategoryCityDatabase : RoomDatabase() {

    abstract val categoryDao: NewTelCategoryCityDao

    companion object {

        private const val USER_DB_NAME = "syy_o_new_tel_category_city_db"

        @Volatile
        private var instance: NewTelCategoryCityDatabase? = null

        @Synchronized
        fun getInstance(context: Context): NewTelCategoryCityDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                NewTelCategoryCityDatabase::class.java,
                USER_DB_NAME
            ).build().also { instance = it }
        }
    }
}

////1、entity的定义
//@Parcelize
//@Entity(tableName = "new_tel_category_city_db")
//@Keep
//data class NewTelCategoryCityBean(
//    @PrimaryKey(autoGenerate = true)
//    val _id: Int,//主键
//    val id: String,
//    val fcaid: Int,
//    val name: String?,
//    val seq: Int,
//    val image: String?,
//    var isSelcted: Boolean,
//) : Parcelable, TextProvider {
//    override fun provideText(): String {
//        return name ?: ""
//    }
//
//}


//2、dao层的定义
@Dao
interface NewTelCategoryCityDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFindCategory(categoryBean: NewTelCategoryBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCategory(categoryBean: NewTelCategoryBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendCategory(categoryBean: List<NewTelCategoryBean>)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateFindCategory(categoryBean: NewTelCategoryBean)

    @Delete
    fun deleteFindCategory(categoryBean: NewTelCategoryBean)

    @Query("delete from new_tel_category_db")
    fun deleteAllCategory()

    @Query("delete from new_tel_category_db")
    suspend fun deleteSuspendAllCategory()

    // = , in , like
    @Query("select * from new_tel_category_db where _id=:id")
    fun queryLiveFindCategory(id: Int = 0): LiveData<NewTelCategoryBean>

    @Query("select * from new_tel_category_db")
    fun queryLiveFindAllCategory(): LiveData<List<NewTelCategoryBean>>

//    @Query("select * from category_db where _id=:id")
//    fun queryFindCategory(id: Int = 0): CategoryBean?

//    @Query("select * from category_db")
//    fun queryFindAllCategory(): List<CategoryBean>?

    @Query("select * from new_tel_category_db where _id =:id")
    suspend fun getFindCategory(id: Int = 0): NewTelCategoryBean?

    @Query("select * from new_tel_category_db")
    suspend fun getAllCategory(): List<NewTelCategoryBean>?
}



